package com.newsproject.service.impl;

import Util.ValidationUtil;
import com.newsproject.mapper.CodeMapper;
import com.newsproject.pojo.Result;
import com.newsproject.pojo.VerificationCode;
import com.newsproject.service.EmailService;
import com.newsproject.service.UserService;
import com.newsproject.service.VerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.Random;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private VerificationCodeService verificationCodeService;

    @Autowired
    private CodeMapper codeMapper;

    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private UserService userService;

    @Override
    public Result<VerificationCode> sendEmail(String email,HttpSession session) {
        Integer i = userService.selectUserIdByEmail(email);
        Result<VerificationCode> result = new Result<>();
        if(!ValidationUtil.isValidEmail(email)){
            result.setMessage("邮箱格式错误");
            result.setCode(400);
            return result;
        }
        if(i != null){
            result.setMessage("邮箱已存在");
            result.setCode(400);
            return result;
        }
        String code = String.format("%06d", new Random().nextInt(999999));
        if( !verificationCodeService.canSendVerificationCode(session)){
            result.setMessage("验证码发送失败，请稍后再试");
            result.setCode(400);
            return result;
        }
        /*
        * 发送验证码
        * */
        SimpleMailMessage message = new SimpleMailMessage();
        // 邮件发送者
        message.setFrom("1990027517@qq.com");
        // 邮件接收者
        message.setTo(email);
        // 邮件主题
        message.setSubject("您的验证码");
        message.setText("验证码：" + code + "，5分钟内有效。");
        try{
            mailSender.send(message);
        } catch (MailException e) {
            result.setCode(400);
            result.setMessage("验证码发送失败");
            return result;
        }
        //将验证码存到session中
        session.setAttribute("code", code);
        session.setAttribute("createdTime", LocalDateTime.now());
        result.setMessage("success");
        result.setCode(200);
        return result;
    }

@Override
public void sendEmail(String email, String subject, String content) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("1990027517@qq.com");
    message.setTo(email);
    message.setSubject(subject);
    message.setText(content);
    mailSender.send(message);
}

    @Override
    public VerificationCode getVerificationCode(String email) {
        Optional<VerificationCode> verificationCodeByEmail = codeMapper.getVerificationCodeByEmail(email);
        return verificationCodeByEmail.orElse(null);
    }

    @Override
    public Result<String> sendCode(String email,HttpSession session) {
        Result<String> result = new Result<>();

        //
        if(!verificationCodeService.canSendVerificationCode(session)||!ValidationUtil.isValidEmail(email)){
            result.setCode(400);
            result.setMessage("验证码发送失败，请稍后再试");
            return result;
        }
        // 生成6位随机验证码
        String code = String.format("%06d", new Random().nextInt(999999));
        // 发送验证码
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1990027517@qq.com");
        message.setTo(email);
        message.setSubject("您的验证码");
        message.setText("验证码：" + code + "，5分钟内有效。");
        //发送验证码
        mailSender.send(message);
        //存储到session中,有效时间为5分钟
        session.setAttribute("code",code);
        session.setAttribute("createdTime",LocalDateTime.now());
        session.setMaxInactiveInterval(300);
        result.setMessage("发送成功");
        result.setCode(200);
        return result;
    }



}
